//
//  RegisterViewController.m
//  OneCreditTreasure
//
//  Created by 夏克镭 on 16/8/25.
//
//

#import "RegisterViewController.h"
#import "CheckCodeTableViewCell.h"
#import "CarInfoTableViewCell.h"

@interface RegisterViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CheckCodeTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) UIButton *registerBtn;

@property (nonatomic, strong) UITextField *phoneNumTF;
@property (nonatomic, strong) UITextField *checkCodeTF;
@property (nonatomic, strong) UITextField *pwdTF;
@property (nonatomic, strong) UITextField *pwdCheckTF;
@end

@implementation RegisterViewController
#pragma mark - CheckCodeTableViewCell delegate
- (void)getCheckCode:(UITableViewCell *)cell {
    [self.view showWarning:@"正在获取验证码,请耐心等待!"];
}

#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.phoneNumTF) {
        [self.checkCodeTF becomeFirstResponder];
    }
    if (textField == self.checkCodeTF) {
        [self.pwdTF becomeFirstResponder];
    }
    if (textField == self.pwdTF) {
        [self.pwdCheckTF becomeFirstResponder];
    }
    if (textField == self.pwdCheckTF) {
        [textField resignFirstResponder];
    }
    return YES;
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CheckCodeTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CheckCodeCell" forIndexPath:indexPath];
        Cell.leftInfoLB.text = self.dataArr[indexPath.section];
        self.phoneNumTF = Cell.inputTF;
        Cell.inputTF.delegate = self;
        Cell.delegate = self;
        return Cell;
    } else {
        CarInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"inputCell" forIndexPath:indexPath];
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Cell.leftLB.text = self.dataArr[indexPath.section];
        Cell.infoTF.delegate = self;
        
        if (indexPath.section == 1) {
            self.checkCodeTF = Cell.infoTF;
            Cell.infoTF.placeholder = @"请输入获取到的验证码";
        }
        if (indexPath.section == 2) {
            self.pwdTF = Cell.infoTF;
            Cell.infoTF.placeholder = @"请保证两次输入的密码一致";
            Cell.infoTF.secureTextEntry = YES;
        }
        if (indexPath.section == 3) {
            self.pwdCheckTF = Cell.infoTF;
            Cell.infoTF.placeholder = @"请保证两次输入的密码一致";
            Cell.infoTF.secureTextEntry = YES;
            Cell.infoTF.returnKeyType = UIReturnKeyDefault;
        }
        return Cell;
    }
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * AUTO_WIDTH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5 * AUTO_WIDTH;
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRGBA(234, 234, 234, 1);
    [self tableView];
    [self registerBtn];
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"注册";
    }
    return self;
}

#pragma mark - 方法 methods
- (void)registerMethod:sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"register" object:self userInfo:@{@"注册成功":@"success"}];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        CGFloat height = kScreenH / 2;
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(0);
            make.height.equalTo(height);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [_tableView registerClass:[CheckCodeTableViewCell class] forCellReuseIdentifier:@"CheckCodeCell"];
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"inputCell"];
    }
    return _tableView;
}

- (NSArray *)dataArr {
    if(_dataArr == nil) {
        _dataArr = @[@"手机号码:",
                     @"输入验证码:",
                     @"输入密码:",
                     @"确认密码:"];
    }
    return _dataArr;
}

- (UIButton *)registerBtn {
    if(_registerBtn == nil) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_registerBtn];
        [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(80);
            make.right.equalTo(-80);
            make.top.equalTo(self.tableView.mas_bottom).equalTo(0);
            make.height.equalTo(40);
        }];
        
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor redColor] CGColor], [UIColor redColor], 8, _registerBtn);
        
        [_registerBtn addTarget:self action:@selector(registerMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

@end
